package com.example.springboot.controller;

import com.example.springboot.Service.CourseSave;
import com.example.springboot.entity.Course;
import com.example.springboot.entity.Student;
import com.example.springboot.entity.Teacher;
import com.example.springboot.mapper.CourseMapper;
import com.example.springboot.mapper.DepartmentMapper;
import com.example.springboot.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RequestMapping("/teacher")
@RestController
public class TeacherController {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private CourseSave courseSave;
//
//    @GetMapping()
//    public List<Teacher> index(){
//        return teacherMapper.findAll();
//    }

//    管理员教师信息接口
    @GetMapping("/msg")
    public   Map<String,Object> teacherMsg(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        pageNum = (pageNum - 1)*pageSize;
        Map<String,Object> res = new HashMap<>();
        List<Teacher> teachers = teacherMapper.findAll(pageSize,pageNum);
        teachers.forEach(teacher -> teacher.setDepartment_name(departmentMapper.findDepartement_name(teacher.getDepartment_id())));
        res.put("data",teachers);
        res.put("total",teacherMapper.findCount());
        res.put("departement",departmentMapper.findAllName());
        return res;
    }
//
//    教师课程查询
    @GetMapping("/teacherCourse")
    public List<Course> teacherCourase(@RequestParam Integer teacher_id,@RequestParam Integer grade){
//       课程号 课程名  学分学时 分类 上课时段 课程安排 人数 课程介绍
        List<Course> courses =courseMapper.teacherCourse(teacher_id,grade);
        courses.forEach(course -> {
            course.setCourse_stuTime(courseSave.getTime(course.getCourse_id()));
        });
        return courses;
    }
    @GetMapping("/teacherCourse_id")
    public List<Course> teacherCourseID(@RequestParam Integer teacher_id  ,@RequestParam String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        if (pattern.matcher(str).matches()){
//              是数字
            List<Course> courses =courseMapper.teacherCourse_id(teacher_id,str);
            courses.forEach(course -> {
                course.setCourse_stuTime(courseSave.getTime(course.getCourse_id()));
            });
            return courses;
        }else {
            List<Course> courses =courseMapper.CourseCourseName(teacher_id,str);
            courses.forEach(course -> {
                course.setCourse_stuTime(courseSave.getTime(course.getCourse_id()));
            });
            return courses;
        }


    }
    @GetMapping("/teacherPeson")
    public Teacher teacherPeson(@RequestParam Integer teacher_id){
        Teacher teacher = teacherMapper.fondPeson(teacher_id);
        teacher.setDepartment_name(departmentMapper.findDepartement_name(teacher.getDepartment_id()));
        return teacher;
    }
    @PostMapping("setTeacher")
    public int setStudent(@RequestBody Teacher teacher){
        return teacherMapper.update_teacher(teacher);
    }
    @GetMapping("likeTeacher")
    public List<Teacher> Like(@RequestParam String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        if (pattern.matcher(str).matches()){
//              是数字
            return teacherMapper.teacher_idSelect(str);
        }else {
            return teacherMapper.teacher_nameSelect("%"+str+"%");
        }
    }
    @GetMapping("getTeacherDepa")
    public List<Teacher> getStudentSper(@RequestParam String department_name){
        return teacherMapper.getTeacherDepa(department_name);

    }


}
